package com.md.model.system;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.md.base.EnumBase;
import com.md.base.exception.ErrorMessage;
import com.md.util.ObjectUtil;
import com.md.util.StringUtil;
import com.md.value.constants.CodeConstants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 专门返回给前端的实体类
 * 2021-09-16 22:23
 *
 * @author 刘骄阳
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class CommonResult<T> {
  /**
   * 状态码
   */
  private Integer code;

  /**
   * 返回消息
   */
  private String message;

  /**
   * 返回数据
   */
  private T data;

  /**
   * 当前时间
   */
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
  @JsonDeserialize(using = LocalDateTimeDeserializer.class)
  @JsonSerialize(using = LocalDateTimeSerializer.class)
  private LocalDateTime date;

  /**
   * 三参的构造方法
   *
   * @param code    状态码
   * @param message 返回消息
   * @param data    返回数据
   */
  public CommonResult(final Integer code, final String message, final T data) {
    this.code = code;
    this.message = message;
    this.data = data;
    this.date = LocalDateTime.now();
  }

  /**
   * 自定义返回
   *
   * @param code    状态码
   * @param message 返回消息
   * @param data    返回数据
   * @return CommentResult对象
   */
  public static <T> CommonResult<T> result(final Integer code, final String message, final T data) {
    return new CommonResult<>(code, message, data);
  }

  /**
   * 操作成功[200]
   *
   * @return CommentResult对象
   */
  public static <T> CommonResult<T> success() {
    return new CommonResult<>(CodeConstants.SUCCESS_CODE, "操作成功!", null);
  }

  /**
   * 操作成功[200]
   *
   * @param data 返回数据
   * @return CommentResult对象
   */
  public static <T> CommonResult<T> success(final T data) {
    return new CommonResult<>(CodeConstants.SUCCESS_CODE, "操作成功!", data);
  }

  /**
   * 操作失败[500]
   *
   * @return CommentResult对象
   */
  public static <T> CommonResult<T> error() {
    return new CommonResult<>(CodeConstants.ERROR_CODE, "操作失败!", null);
  }

  public static <T> CommonResult<T> error(final EnumBase error) {
    return new CommonResult<>(error.getCode(), error.getMessage(), null);
  }

  public static <T> CommonResult<T> error(final ErrorMessage error) {
    return new CommonResult<>(error.getCode(), error.getMessage(), null);
  }

  public boolean isSuccess() {
    return ObjectUtil.equals(this.code, CodeConstants.SUCCESS_CODE);
  }

  /**
   * 重写toString
   *
   * @return 序列化之后的对象
   */
  @Override
  public String toString() {
    return StringUtil.objectToJson(this);
  }
}
